package dataStructure.xmg1.a11.tree;

//import dataStructure.xmg1.a10.printer.BinaryTreeInfo;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: vince_zww
 * @date: 2020/12/29 21:29
 * @desceription: 二叉搜索树
 */
public class BinaryTree<E>{
    protected int size;
    protected Node<E> root;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void clear() {
        root = null;
        size = 0;
    }

    //前序遍历
    public void preorder(Visitor<E> visitor) {
        preorder(root, visitor);
    }

    private void preorder(Node<E> node, Visitor<E> visitor) {
        if (node == null || visitor == null) return;
        visitor.visit(node.element);
        preorder(node.left, visitor);
        preorder(node.right, visitor);
    }

    //中序遍历
    public void inorder(Visitor<E> visitor) {
        inorder(root, visitor);
    }

    private void inorder(Node<E> node, Visitor<E> visitor) {
        if (node == null || visitor == null) return;
        inorder(node.left, visitor);
        visitor.visit(node.element);
        inorder(node.right, visitor);
    }

    //后序遍历
    public void postorder(Visitor<E> visitor) {
        postorder(root, visitor);
    }

    private void postorder(Node<E> node, Visitor<E> visitor) {
        if (node == null || visitor == null) return;
        postorder(node.left, visitor);
        postorder(node.right, visitor);
        visitor.visit(node.element);
    }


    //层序遍历
    public void levelOrder(Visitor<E> visitor) {
        if (root == null || visitor == null) return;
        Queue<Node<E>> queue = new LinkedList<>();
        //入队
        queue.offer(root);
        while (!queue.isEmpty()) {
            //出队
            Node<E> node = queue.poll();
            visitor.visit(node.element);
            if (node.left != null) {
                //入队
                queue.offer(node.left);
            }
            if (node.right != null) {
                //入队
                queue.offer(node.right);
            }
        }
    }

    //判断是否完全二叉树——第二种写法
    public boolean isCompare(){
        if (root == null)return false;

        Queue<Node<E>> queue = new LinkedList<>();
        queue.offer(root);

        boolean leaf = false;
        while (!queue.isEmpty()) {
            Node<E> node = queue.poll();

            if (leaf && !node.isLeaf()) return false;

            //左边判断
            if (node.left != null) {
                queue.offer(node.left);
            } else if (node.right != null) {
                //node.left == null && node.right != null
                return false;
            }

            //右边判断
            if (node.right != null) {
                queue.offer(node.right);
            } else {
                //node.left == null && node.right == null
                //node.left != null && node.right == null
                leaf = true;
            }
        }
        return true;
    }

    //递归方式
    public int height() {
        return height(root);
    }

    private int height(Node<E> node) {
        if (node ==null) return 0;
        return 1 + Math.max(height(node.left), height(node.right));
    }

    //迭代方式-层序遍历方式
    public int height2() {
        if (root == null) return 0;
        //树的高度
        int height = 0;
        //存储着每一层的元素数量
        int levelSize = 1;
        Queue<Node<E>> queue = new LinkedList<>();
        //入队
        queue.offer(root);
        while (!queue.isEmpty()) {
            //出队
            Node<E> node = queue.poll();
            //这一层的一个元素出队，这一层的元素数量就减一
            levelSize--;
            if (node.left != null) {
                //入队
                queue.offer(node.left);
            }
            if (node.right != null) {
                //入队
                queue.offer(node.right);
            }
            if (levelSize == 0) {//意味着即将要访问下一层
                levelSize = queue.size();
                //这一层访问结束，开始下一层，所以高度要加一
                height++;
            }
        }
        return height;
    }

    protected Node<E> createNode(E element, Node<E> parent) {
        return new Node<>(element, parent);
    }

    //前驱节点
    protected Node<E> predecessor(Node<E> node){
        if (node == null) return null;

        //前驱结点在左子树当中(left.right.right.right...)
        Node<E> p = node.left;
        if (p != null) {
            while (p.right != null) {
                p = p.right;
            }
            return p;
        }
        //从祖父节点中寻找
        while (node.parent != null && node == node.parent.left) {
            node = node.parent;
        }
        //node.parent == null
        //node == node.parent.right

        return node.parent;
    }

    //后继节点
    protected Node<E> successor(Node<E> node){
        if (node == null) return null;

        //前驱结点在左子树当中(right.left.left.left...)
        Node<E> p = node.right;
        if (p != null) {
            while (p.left != null) {
                p = p.left;
            }
            return p;
        }
        //从祖父节点中寻找
        while (node.parent != null && node == node.parent.right) {
            node = node.parent;
        }

        return node.parent;
    }

    //访问器
//    public static interface Visitor<E> {
//        void visit(E element);
//    }

    public static abstract class Visitor<E>{
        boolean stop;
        public abstract boolean visit(E element);
    }

    protected static class Node<E> {
        E element;
        Node<E> left;
        Node<E> right;
        Node<E> parent;

        public Node(E element, Node<E> parent) {
            this.element = element;
            this.parent = parent;
        }

        public boolean isLeaf() {
            return left == null && right == null;
        }

        public boolean hasTwoChildern() {
            return left != null && right != null;
        }

        public boolean isLeftChild() {
            return parent != null && this == parent.left;
        }

        public boolean isRightChild() {
            return parent != null && this == parent.right;
        }

        public Node<E> sibling(){
            if (isLeftChild()) {
                return parent.right;
            }
            if (isRightChild()) {
                return parent.left;
            }

            return null;
        }
    }





}
